﻿#if UNITY_EDITOR

using UnityEngine;

namespace AI.Graph.Editor {


    public enum MouseButton {

        Any = -1,
        Left = 0,
        Right = 1,
        Middle = 2

    }


    public static class EventExtension {

        /// <summary>
        /// Returns true if the mouse is inside the given rect. 
        /// </summary>
        public static bool IsMouseInsideRect (this Event e, Rect rect) {
            return rect.Contains (e.mousePosition);
        }


        /// <summary>
        /// Returns true if the event is a mouse down event.
        /// </summary>
        public static bool IsMouseDown (this Event e, MouseButton mouseButton, bool rawType = false) {
            if (mouseButton != MouseButton.Any && e.button != (int) mouseButton) return false;

            if (rawType) {
                return e.rawType == EventType.MouseDown;
            }

            return e.type == EventType.MouseDown;
        }


        /// <summary>
        /// Returns true if the event is a mouse drag event.
        /// </summary>
        public static bool IsMouseDrag (this Event e, MouseButton mouseButton, bool rawType = false) {
            if (mouseButton != MouseButton.Any && e.button != (int) mouseButton) return false;

            if (rawType) {
                return e.rawType == EventType.MouseDrag;
            }

            return e.type == EventType.MouseDrag;
        }


        /// <summary>
        /// Returns true if the event is a mouse up event.
        /// </summary>
        public static bool IsMouseUp (this Event e, MouseButton mouseButton, bool rawType = false) {
            if (mouseButton != MouseButton.Any && e.button != (int) mouseButton) return false;

            if (rawType) {
                return e.rawType == EventType.MouseUp;
            }

            return e.type == EventType.MouseUp;
        }


        /// <summary>
        /// Returns true if the event is a mouse move event.
        /// </summary>
        public static bool IsMouseMove (this Event e) {
            return e.type == EventType.MouseMove;
        }


        /// <summary>
        /// Returns true if the event is a key down event.
        /// </summary>
        public static bool IsKeyDown (this Event e, KeyCode keyCode, bool rawType = false) {
            if (rawType) {
                return e.rawType == EventType.KeyDown && e.keyCode == keyCode;
            }

            return e.type == EventType.KeyDown && e.keyCode == keyCode;
        }


        /// <summary>
        /// Returns true if the event is a key up event.
        /// </summary>
        public static bool IsKeyUp (this Event e, KeyCode keyCode, bool rawType = false) {
            if (rawType) {
                return e.rawType == EventType.KeyUp && e.keyCode == keyCode;
            }

            return e.type == EventType.KeyUp && e.keyCode == keyCode;
        }


        /// <summary>
        /// Returns true if the event is a repaint event.
        /// </summary>
        public static bool IsRepaintEvent (this Event e) {
            return e.type == EventType.Repaint;
        }

    }


}
#endif